﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace RatpConsole
{
    public partial class ConsoleForm : Form
    {
        public ConsoleForm()
        {
            InitializeComponent();
        }

        private void ConsoleForm_Load(object sender, EventArgs e)
        {
            
            // Initialize Combo
            comboBoxProbe.DataSource = ProbeManager.getInstance().listProbes();
            comboBoxProbe.DisplayMember = "DisplayName";
            comboBoxProbe.ValueMember = "Id";
            // Initialize Refresh UI
            timerUI.Enabled = true;
            
        }

        private void ConsoleForm_Deact(object sender, EventArgs e)
        {

        }

        private void comboBoxProbe_Change(object sender, EventArgs e)
        {
            Probe currentProbe = (Probe)comboBoxProbe.SelectedItem;
            currentProbe.refresh();
            syncUIWithProbe(currentProbe);
        }

        private void buttonGrab_Click(object sender, EventArgs e)
        {
            Probe currentProbe = (Probe) comboBoxProbe.SelectedItem;
            currentProbe.refresh();
            syncUICells(currentProbe);
        }

        // ==============================================================  UI Refresh

        private void timerUI_Tick(object sender, EventArgs e)
        {
            if (!comboBoxProbe.DroppedDown)
            {
                Probe currentProbe = (Probe)comboBoxProbe.SelectedItem;
                syncUIWithProbe(currentProbe);
                String datePatt = @"M/d/yyyy hh:mm:ss tt";
                textBoxConsole.Text = DateTime.Now.ToString(datePatt) + "// UI Refreshed \r\n";
            }
        }


        // ==============================================================  UI Shortcuts

        private void syncUIWithProbe(Probe probe)
        {
            textBoxBusDirection.Text = probe.BusDirection;
            textBoxBusName.Text = probe.BusLine;
            textBoxBusStop.Text = probe.BusStop;
            textBoxURL.Text = probe.BusUrl;
            syncUICells(probe);
        }

        private void syncUICells(Probe probe)
        {
            String[] values = probe.getFields();
            textBoxHeader1.Text = values[0];
            textBoxValue1.Text = values[1];
            textBoxHeader2.Text = values[2];
            textBoxValue2.Text = values[3];
            checkBoxField2.Checked = probe.NumberInField2;
            checkBoxField4.Checked = probe.NumberInField4;
        }


        // ==============================================================  Minimize to TRAY
        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void ConsoleForm_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                notifyIconTray.Visible = true;
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                notifyIconTray.Visible = false;
                this.Show();
            }
        }

        

        
        
    }
}
